//
//  MasterViewController.m
//  DaleTou
//
//  Created by  on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "MasterViewController.h"
#import "KaijiangTableCell.h"
#import "MasterModel.h"
#import "DLTModel.h"
#import <MJRefresh.h>

@interface MasterViewController ()<UITableViewDelegate,UITableViewDataSource> {
    // 往期号码
    
    NSMutableArray *_pastCorrectNum;
    // 上次中奖
    NSString *_last;
}

@property (weak, nonatomic) IBOutlet UIView *inforView;
@property (weak, nonatomic) IBOutlet UILabel *ball;
@property (weak, nonatomic) IBOutlet UILabel *openCount;
@property (weak, nonatomic) IBOutlet UILabel *openNum;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MasterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self setUI];
}

- (void)initData {
    _pastCorrectNum = [NSMutableArray array];
    [self getCorrectNum];
}

- (void)setUI {
    self.title = @"大师预测";
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"F4F4F4"];
    MaterListTableCell *cell = [MaterListTableCell defaultClassNameNibView];
    cell.frame = CGRectMake(0, 0, self.view.bounds.size.width + 50, 80);
    cell.model = self.masterInfo;
    cell.ranking.hidden = YES;
    [self.inforView addSubview:cell];
    
    self.ball.text = @[@"蓝球定二",@"蓝球杀三",@"红球杀三",@"红球定二"][self.index];
    self.openCount.text = self.masterInfo.openCount;
    self.openNum.text = [NSString stringWithFormat:@"%@,%@",self.masterInfo.forecastNumList[0][0],self.masterInfo.forecastNumList[0][1]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    DLTModel *model = _pastData[0];
    _openCount.text = [NSString stringWithFormat:@"第%@期",model.expect];
}

- (void)loadMore
{
    [self.pastData addObjectsFromArray:self.pastData];
}


- (void)getCorrectNum {
    for (int i = 0; i < _pastData.count; i ++) {
        NSInteger num = 0;
        for (NSString *str in self.masterInfo.forecastNumList[i]) {
            DLTModel *model = _pastData[i];
            if ([model.opencode containsString:str]) {
                num ++;
                _last = _pastData[i];
            }
        }
        [_pastCorrectNum addObject:[NSString stringWithFormat:@"中%ld",num]];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_pastData.count > 4) {
        return 4;
    } else {
        return _pastData.count;
    }
//    return self.masterInfo.forecastNumList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const idS = @"KaijiangTableCell";
    KaijiangTableCell *cell = [tableView dequeueReusableCellWithIdentifier:idS];
    if (!cell) {
        cell = [KaijiangTableCell defaultClassNameNibView];
    }
    DLTModel *model = _pastData[indexPath.row];
    cell.num.text = model.opencode;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.date.text = [NSString stringWithFormat:@"第%@期",model.expect];
    cell.correct.text = _pastCorrectNum[indexPath.row];
    cell.forecast.text = [NSString stringWithFormat:@"%@,%@",self.masterInfo.forecastNumList[indexPath.row][0],self.masterInfo.forecastNumList[indexPath.row][1]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

@end
